import { defineConfig, type UserConfigFn, loadEnv } from 'vite'

import vue from '@vitejs/plugin-vue'

import Inspect from 'vite-plugin-inspect'

import AutoImport from 'unplugin-auto-import/vite'

import Components from 'unplugin-vue-components/vite'

import { VantResolver } from 'unplugin-vue-components/resolvers'

import Jsx from '@vitejs/plugin-vue-jsx'

import tailwindcss from "tailwindcss"

import { wrapperEnv, autoAliasEnv } from './extensions/utils'

import path from "path"

// https://vitejs.dev/config/
const viteConfig: UserConfigFn = (env) =>  {
  const viteEnv = wrapperEnv(loadEnv(env.mode, process.cwd()))
  return {
    define: {
      NODE_ENV: JSON.stringify(env.mode),
      ...(autoAliasEnv(viteEnv) || {})
    },
    plugins: [
      vue(),
      AutoImport({
        include: [
          /\.vue$/, /\.vue\?vue/, 
          /\.jsx$/, /\.jsx\?jsx/, 
          /\.tsx$/, /\.tsx\?tsx/, 
        ],
        imports: ["vue"],
        eslintrc: { 
          enabled: true,
          filepath: "./.eslintrc-auto-import.json",
          globalsPropValue: true
        },
        dts: 'src/types/auto-imports.d.ts'
      }),
      Components({
        resolvers: [VantResolver()],
        dts: "src/types/components.d.ts",
      }),
      Jsx(),
      Inspect(),
    ],
    build: {},
    server: {
      port: 8080,
      host: '0.0.0.0',
      proxy: {}
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    css: {
      postcss: {
        plugins: [
          tailwindcss(),
        ],
      },
    }
  }
}
export default defineConfig(viteConfig)
